package com.example.campuslink.ui.activity

import android.content.Intent
import android.view.View
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.adpter.MineInvitationAdapter
import com.example.campuslink.commen.adpter.UserMainInvitationAdapter
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpKey
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.databinding.ActivityUserMainBinding
import com.example.campuslink.viewModel.UserMainViewModel

class UserMainActivity : BaseActivity<ActivityUserMainBinding, UserMainViewModel>(
    UserMainViewModel::class.java,
    R.layout.activity_user_main,
    BR.viewModel
) {
    private var adapter: UserMainInvitationAdapter? = null
    private var nowUserNumber = ""
    override fun initView() {
        init()
        val userNumber = intent.getStringExtra(putExtraKey.USER_NUMBER.key)
        if (userNumber == Sp.getString(this, SpKey.USER_NUMBER.key, "")) {
            mViewDataBinding?.btnAttention?.visibility = View.GONE
            mViewDataBinding?.btnAttention?.isEnabled = false
        }
        mViewModel?.getUserInfo(userNumber ?: "")
        mViewModel?.getUserNumberInfo(userNumber ?: "")
        mViewModel?.getAttentionInvitationList(userNumber ?: "")
        mViewModel?.getFenSiteList(userNumber ?: "")
        mViewModel?.userInfo?.observe(this) {
            mViewDataBinding?.tvUserNumber?.text = it?.userNumber
            mViewDataBinding?.tvUserName?.text = it?.userName
            Glide.with(this).load(it?.userAvatar)
                .apply(RequestOptions.circleCropTransform())
                .error(R.drawable.ic_empty)
                .into(mViewDataBinding?.ivUserImg!!)
            if (it?.userIntro == "") {
                mViewDataBinding?.tvCommend?.text = getString(R.string.user_commend)
            } else {
                mViewDataBinding?.tvCommend?.text = it?.userIntro
            }
            if (it?.userGender == "male") {
                Glide.with(this).load(R.drawable.male)
                    .apply(RequestOptions.circleCropTransform())
                    .error(R.drawable.no_sex)
                    .into(mViewDataBinding?.ivSex!!)
            } else if (it?.userGender == "woman") {
                Glide.with(this).load(R.drawable.woman)
                    .apply(RequestOptions.circleCropTransform())
                    .error(R.drawable.no_sex)
                    .into(mViewDataBinding?.ivSex!!)
            }
        }
        mViewModel?.userNumber?.observe(this) {
            mViewDataBinding?.tvSchoolName?.text = "学校为:${it.schoolName}"
        }
        mViewDataBinding?.ivBack?.setOnClickListener({
            finish()
        })
        mViewModel?.attentionInvitationLiveData?.observe(this) {
            mViewDataBinding?.tvAttentionCount?.text = it.size.toString()
        }
        mViewModel?.fenSiteLiveData?.observe(this) {
            mViewDataBinding?.tvFansNumber?.text = it.size.toString()
        }
        mViewDataBinding?.ivShare?.setOnClickListener({
            val textToShare = "为您推荐的校园信息共享平台账号为: ${userNumber} 快点来关注他吧"
            val intent = Intent(Intent.ACTION_SEND)
            intent.setType("text/plain")
            intent.putExtra(Intent.EXTRA_TEXT, textToShare)
            startActivity(Intent.createChooser(intent, "分享账号"))
        })
        mViewDataBinding?.ivBack?.setOnClickListener {
            finish()
        }
        mViewDataBinding?.layoutFans?.setOnClickListener({
            mViewModel?.jumpToFansAndAttentionActivity(putExtraKey.FANS.key, nowUserNumber)
        })
        mViewDataBinding?.layoutAttention?.setOnClickListener({
            mViewModel?.jumpToFansAndAttentionActivity(putExtraKey.ATTENTION.key, nowUserNumber)
        })
        mViewModel?.publicInvitationLiveData?.observe(this) {
            adapter?.setListData(it.reversed())
        }
        mViewModel?.publicInvitationLiveData?.observe(this) {
            adapter?.setListData(it.reversed())
        }
        mViewModel?.attentionInvitationLiveData?.observe(this) {
            if (it.contains(nowUserNumber)) {
                mViewDataBinding?.btnAttention?.text = "已关注"
                if (mViewModel?.fenSiteLiveData?.value?.contains(
                        nowUserNumber
                    ) ?: false
                ) {
                    mViewDataBinding?.btnAttention?.text = "朋友"
                }
            } else {
                mViewDataBinding?.btnAttention?.text = "关注"
            }
        }
        mViewDataBinding?.btnAttention?.setOnClickListener({
            if (mViewDataBinding?.btnAttention?.text == "已关注"
                || mViewDataBinding?.btnAttention?.text == "朋友"
            ) {
                mViewModel?.removeAttentionAndFans(nowUserNumber)
            } else {
                mViewModel?.addAttentionAndFans(nowUserNumber)
            }
        })
        mViewDataBinding?.rvInvitationShow?.adapter = adapter
        mViewDataBinding?.rvInvitationShow?.layoutManager = GridLayoutManager(this, 3)
//        mViewDataBinding?.btnAttention?.setOnClickListener({
//            mViewModel?.addAttentionAndFans()
//        })
    }

    fun init() {
        mViewModel?.context = this
        mViewModel?.initDB(this)
        adapter = UserMainInvitationAdapter(mViewModel!!)
        nowUserNumber = intent.getStringExtra(putExtraKey.USER_NUMBER.key) ?: ""
        mViewModel?.getPublicInvitationList(nowUserNumber)
    }
}